/*
 * @Date: 2022-07-27 14:29:23
 * @LastEditors: Zzt
 * @LastEditTime: 2022-08-08 18:26:51
 * @FilePath: \archives-fe\src\view\sysMaintenance\TaskCronE\reducers\TaskCronEReducer.js
 */
import {
  ADDPLAN,
  DELETE_EXPORT_ITEM,
  SET_SELECTED_ITEM,
  AMEND_PLAN,
  VISIBLE,
  EDIT_PLAN,
  SEARCH
} from '../constants';
import { delItemByIdArray } from '@/utils/list';
import { arrToTree } from '@/utils';
export const TaskCronEReducer = (state, action) => {
  switch (action.type) {
    case ADDPLAN: {
      const _plan = JSON.parse(JSON.stringify(state.task));
      _plan.push(action.data);
      console.log(action.data);
      return {
        ...state,
        task: _plan
      };
    }
    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectId: action.data };
    case DELETE_EXPORT_ITEM: {
      console.log('del_export', action.ids);
      return {
        ...state,
        task: delItemByIdArray(action.ids, state.task)
      };
    }
    // 选中修改项并复制给choice
    case AMEND_PLAN: {
      console.log(action.data);
      const choice = state.task.find((item) => {
        return item.id === action.data;
      });
      console.log(choice);
      return {
        ...state,
        choice,
        isVisible: true
      };
    }
    // 修改可视化的值
    case VISIBLE: {
      return {
        ...state,
        isVisible: action.data
      };
    }
    // 修改
    case EDIT_PLAN: {
      console.log(action.data.id);
      const ToDo = JSON.parse(JSON.stringify(state.task));
      return {
        ...state,
        task: ToDo.map((item) => {
          if (item.id === action.data.id) {
            item = action.data;
          }
          return item;
        })
      };
    }
    // 查询
    case SEARCH: {
      const Search = JSON.parse(JSON.stringify(state.task));
      const newArr = [];
      // console.log(action.data);
      // if (action.data === 1 || action.data === '') {
      //   newArr = Search;
      // } else {
      //   newArr = Search.filter((item) => {
      //     return item.taskname === action.data;
      //   });
      // }
      for (let i = 0; i < Search.length; i++) {
        if (Search[i].taskname.split(action.data).length > 1) {
          newArr.push(Search[i]);
        }
      }
      return {
        ...state,
        searchList: newArr
      };
    }
    default:
      console.log('untouched', action);
      return state;
  }
};
